package com.AAL.Action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.SpeedDAO;
import com.AAL.DAO.TypeOfAutoDAO;
import com.AAL.DAO.TypesOfWheelDAO;
import com.AAL.DAO.WheelDetailDAO;
import com.AAL.Model.Speed;
import com.AAL.Model.TypeOfAuto;
import com.AAL.Model.TypesOfWheel;
import com.AAL.Model.WheelDetail;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class WheelAction extends ActionSupport implements SessionAware {
	private Map<String, Object> session;
	// phatdt start
	private WheelDetailDAO wheelDetailDAO;
	private List<WheelDetail> listWheelDetails;
	private WheelDetail wheelDetail;
	private List<TypesOfWheel> lstTypesOfWheels;
	private TypesOfWheel typesOfWheel;
	private List<Speed> lstSpeed;
	private TypesOfWheelDAO typesOfWheelDAO;
	private SpeedDAO speedDAO;
	private Speed speed;
	private TypeOfAutoDAO typeOfAutoDAO;
	private List<TypeOfAuto> lstTypeOfAutos;
	private List<WheelDetail> listWheelDetailsSearch;

	public List<WheelDetail> getListWheelDetailsSearch() {
		return listWheelDetailsSearch;
	}

	public void setListWheelDetailsSearch(
			List<WheelDetail> listWheelDetailsSearch) {
		this.listWheelDetailsSearch = listWheelDetailsSearch;
	}

	public TypeOfAutoDAO getTypeOfAutoDAO() {
		return typeOfAutoDAO;
	}

	public void setTypeOfAutoDAO(TypeOfAutoDAO typeOfAutoDAO) {
		this.typeOfAutoDAO = typeOfAutoDAO;
	}

	public List<TypeOfAuto> getLstTypeOfAutos() {
		return lstTypeOfAutos;
	}

	public void setLstTypeOfAutos(List<TypeOfAuto> lstTypeOfAutos) {
		this.lstTypeOfAutos = lstTypeOfAutos;
	}

	public TypesOfWheel getTypesOfWheel() {
		return typesOfWheel;
	}

	public void setTypesOfWheel(TypesOfWheel typesOfWheel) {
		this.typesOfWheel = typesOfWheel;
	}

	public Speed getSpeed() {
		return speed;
	}

	public void setSpeed(Speed speed) {
		this.speed = speed;
	}

	public TypesOfWheelDAO getTypesOfWheelDAO() {
		return typesOfWheelDAO;
	}

	public void setTypesOfWheelDAO(TypesOfWheelDAO typesOfWheelDAO) {
		this.typesOfWheelDAO = typesOfWheelDAO;
	}

	public SpeedDAO getSpeedDAO() {
		return speedDAO;
	}

	public void setSpeedDAO(SpeedDAO speedDAO) {
		this.speedDAO = speedDAO;
	}

	public List<TypesOfWheel> getLstTypesOfWheels() {
		return lstTypesOfWheels;
	}

	public void setLstTypesOfWheels(List<TypesOfWheel> lstTypesOfWheels) {
		this.lstTypesOfWheels = lstTypesOfWheels;
	}

	public List<Speed> getLstSpeed() {
		return lstSpeed;
	}

	public void setLstSpeed(List<Speed> lstSpeed) {
		this.lstSpeed = lstSpeed;
	}

	public WheelDetail getWheelDetail() {
		return wheelDetail;
	}

	public void setWheelDetail(WheelDetail wheelDetail) {
		this.wheelDetail = wheelDetail;
	}

	public WheelDetailDAO getWheelDetailDAO() {
		return wheelDetailDAO;
	}

	public void setWheelDetailDAO(WheelDetailDAO wheelDetailDAO) {
		this.wheelDetailDAO = wheelDetailDAO;
	}

	public List<WheelDetail> getListWheelDetails() {
		return listWheelDetails;
	}

	public void setListWheelDetails(List<WheelDetail> listWheelDetails) {
		this.listWheelDetails = listWheelDetails;
	}

	public WheelAction() {
		this.wheelDetailDAO = new WheelDetailDAO();
		this.typesOfWheelDAO = new TypesOfWheelDAO();
		this.speedDAO = new SpeedDAO();
		this.typeOfAutoDAO = new TypeOfAutoDAO();
	}

	public String viewListProduct() {
		try {
			System.out.println("Goi ham viewListProduct");
			setListWheelDetails(wheelDetailDAO.getAllListWheelDetail());
			setLstTypeOfAutos(typeOfAutoDAO.getAllTypeOfAutos());
			setLstTypesOfWheels(typesOfWheelDAO.getAllTypesOfWheels());
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String viewProductDetail() {
		try {
			System.out.println("Goi ham viewProductDetail");
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			String wheelDetailID = request.getParameter("id").toString();
			wheelDetail = wheelDetailDAO.getWheelDetail(wheelDetailID);

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String viewProductAdd() {
		try {
			System.out.println("Goi ham viewProductAdd");
			lstTypesOfWheels = typesOfWheelDAO.getAllTypesOfWheels();
			lstSpeed = speedDAO.getAllSpeeds();
			System.out.println("end");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String productAdd() {
		try {
			System.out.println("Goi ham productAdd");
			Speed speedT = this.speedDAO.getSpeed(speed.getSpeedID());
			TypesOfWheel typesOfWheelT = this.typesOfWheelDAO
					.getTypesOfWheel(typesOfWheel.getTypeOfWheelID());
			wheelDetail.setSpeed(speedT);
			wheelDetail.setTypesOfWheel(typesOfWheelT);
			System.out.println("getHeight:"
					+ wheelDetail.getTypesOfWheel().getTypeOfWheelID());
			System.out.println("getSpeedID"
					+ wheelDetail.getSpeed().getSpeedID());
			float S = (float) (0.03 * (Math
					.pow(
							(wheelDetail.getDiameter() + (wheelDetail
									.getHeight() * wheelDetail.getWidth()) / 50),
							2)));
			wheelDetail.setOccupiedArea(S);
			wheelDetail = wheelDetailDAO.saveAndUpdateWheelDetail(wheelDetail);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String viewProductEdit() {
		try {
			System.out.println("Goi ham viewProductEdit");
			lstTypesOfWheels = typesOfWheelDAO.getAllTypesOfWheels();
			lstSpeed = speedDAO.getAllSpeeds();
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			String wheelDetailID = request.getParameter("id").toString();
			wheelDetail = wheelDetailDAO.getWheelDetail(wheelDetailID);
			System.out.println("end");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String productEdit() {
		try {
			System.out.println("Goi ham productEdit");
			WheelDetail wheelDetailUpdate = this.getWheelDetail();
			/*
			 * Speed speedT = this.speedDAO.getSpeed(speed.getSpeedID());
			 * TypesOfWheel typesOfWheelT =
			 * this.typesOfWheelDAO.getTypesOfWheel(
			 * typesOfWheel.getTypeOfWheelID());
			 * wheelDetailUpdate.setSpeed(speedT);
			 * wheelDetailUpdate.setTypesOfWheel(typesOfWheelT);
			 */
			System.out.println("getTypeOfWheelID:"
					+ wheelDetail.getTypesOfWheel().getTypeOfWheelID());
			System.out.println("getSpeedID"
					+ wheelDetail.getSpeed().getSpeedID());
			System.out.println("getHeight" + wheelDetailUpdate.getHeight());
			float S = (float) (0.03 * (Math
					.pow(
							(wheelDetail.getDiameter() + (wheelDetail
									.getHeight() * wheelDetail.getWidth()) / 50),
							2)));
			wheelDetailUpdate.setOccupiedArea(S);
			wheelDetailUpdate = this.wheelDetailDAO
					.saveAndUpdateWheelDetail(wheelDetailUpdate);
			wheelDetail = this.wheelDetailDAO.getWheelDetail(wheelDetailUpdate
					.getWheelDetailID());
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String productSearch() {
		try {
			System.out.println("Goi ham productSearch");
			WheelDetail wheelDetailSearch = this.getWheelDetail();
			System.out.println("getTypeOfWheelID:"
					+ wheelDetailSearch.getTypesOfWheel().getTypeOfWheelID());
			System.out.println("getWheelDetailID: "
					+ wheelDetailSearch.getWheelDetailID());
			System.out.println("getTypeOfAutoID: "
					+ wheelDetailSearch.getTypesOfWheel().getTypeOfAuto()
							.getTypeOfAutoID());
			String typeOfWheelID = wheelDetailSearch.getTypesOfWheel()
					.getTypeOfWheelID();
			String wheelDetailID = wheelDetailSearch.getWheelDetailID();
			String typeOfAutoID = wheelDetailSearch.getTypesOfWheel()
					.getTypeOfAuto().getTypeOfAutoID();
			setListWheelDetailsSearch(wheelDetailDAO.getAllListWheelDetailForSearch(wheelDetailID, typeOfWheelID, typeOfAutoID));
			setLstTypeOfAutos(typeOfAutoDAO.getAllTypeOfAutos());
			setLstTypesOfWheels(typesOfWheelDAO.getAllTypesOfWheels());
			// typeOfWheelID, typeOfAutoID));
			System.out.println("end productSearch");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	// phatdt end

	public String productManagement() {
		try {
			System.out.println("Goi ham product management");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}
}
